{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b7f9ce31",
   "metadata": {},
   "source": [
    "# 一、熟悉python基础语法和jupyter notebook的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2b4b112",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">1)输出语句</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1ed8417",
   "metadata": {},
   "outputs": [],
   "source": [
    "print (\"Hello, Python!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5a4b48",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">2)行和缩进</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7268bae8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#python中通过缩进来进行模块编写\n",
    "if True:\n",
    "    print (\"正确的缩进格式\")\n",
    "else:\n",
    "    print (\"False\")\n",
    "print (\"错误的缩进格式\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b920f79",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">3)注释使用</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ac7412a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 单行注释用'#'\n",
    "print (\"单行注释\")  # 第一个注释\n",
    "\n",
    "# 多行注释使用三个单引号或三个双引号\n",
    "\n",
    "'''\n",
    "这是多行注释，使用单引号\n",
    "这是多行注释，使用单引号\n",
    "'''\n",
    "\"\"\"\n",
    "这是多行注释，使用双引号\n",
    "这是多行注释，使用双引号\n",
    "\"\"\"\n",
    "print (\"多行注释\")  # 第二个注释\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e14475e",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">4)代码组</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82836cb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#缩进相同的一组语句构成一个代码块，我们称之代码组\n",
    "num=2\n",
    "if num==1 : \n",
    "   print(\"num为1\") \n",
    "elif num==2 :  \n",
    "   print(\"num为2\")  \n",
    "else :  \n",
    "   print(\"num为3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f44b00c",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">5)变量类型</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3033cb49",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "Python 中的变量赋值不需要类型声明\n",
    "每个变量在内存中创建，都包括变量的标识，名称和数据这些信息\n",
    "每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建\n",
    "'''\n",
    "counter = 13 # 赋值整型变量\n",
    "miles = 130.0 # 浮点型\n",
    "name = \"Taylor\" # 字符串\n",
    " \n",
    "print(counter)\n",
    "print(miles)\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4dfb10c",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">6)input的使用</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed32f1f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#input接收任意任性输入，将所有输入默认为字符串处理，并返回字符串类型\n",
    "a = input(\"input:\")                 # 输入整数\n",
    "print(\"a的类型：\",type(a))             \n",
    "b = input(\"input:\")                 # 输入字符串\n",
    "print(\"b的类型：\",type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cf5752b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#需要输入多个值时，可以考虑string中的split函数\n",
    "r1,r2,r3 = (input(\"input：\").split(\" \"))\n",
    "r1= int(r1)\n",
    "r2= int(r2)\n",
    "r3= int(r3)\n",
    "print(\"r1,r2,r3:\",r1,r2,r3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a4179e7",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">7)函数的使用</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cbd51c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数示例1\n",
    "def hello() :\n",
    "    print(\"Hello Python!\")\n",
    "\n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b751722",
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数示例2\n",
    "# 计算面积函数\n",
    "def area(width, height):\n",
    "    return width * height\n",
    "\n",
    "w = 13\n",
    "h = 130\n",
    "print(\"width =\", w, \" height =\", h, \" area =\", area(w, h))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "992131d9",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">8)列表</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3aa2151",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表的数据项不需要具有相同的类型，只要把逗号分隔的不同的数据项使用方括号括起来即可\n",
    "list = ['Software', 2021 , 'Engineering' , 2022]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c464bfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#逆向索引输出列表\n",
    "print( list[-1] )\n",
    "print( list[-2] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8a7df9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#遍历列表\n",
    "print( list[0:4] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10c6d154",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表修改\n",
    "list[3]=2023\n",
    "print( list )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e4033a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表加入新元素\n",
    "list.append(2024)\n",
    "print( list )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e841d3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表删除元素\n",
    "del list[1]\n",
    "print( list )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20b61f61",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">9)循环</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94b4eb59",
   "metadata": {},
   "outputs": [],
   "source": [
    "#while循环\n",
    "print('while循环：')\n",
    "num=0\n",
    "while num<13 :\n",
    "    print(num)\n",
    "    num += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea934a22",
   "metadata": {},
   "outputs": [],
   "source": [
    "#while-else循环\n",
    "\n",
    "print('while-else循环：')\n",
    "symbol=True\n",
    "while symbol:\n",
    "    print(\"true\")\n",
    "    symbol=False\n",
    "else:\n",
    "    print(\"false\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "820b2a06",
   "metadata": {},
   "outputs": [],
   "source": [
    "#for循环\n",
    "print('for循环：')\n",
    "for i in range(0,13) :\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61d6e4bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#break和continue\n",
    "print('break：')\n",
    "i=9\n",
    "while i > 0:\n",
    "    i -= 1\n",
    "    print(i)\n",
    "    if i == 6: \n",
    "        break\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "046fa5f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('continue：')\n",
    "i=9\n",
    "while i > 0:\n",
    "    i -= 1\n",
    "    if i == 6: \n",
    "        continue\n",
    "    print(i)\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a78b05cb",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">10)模块导入</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d56e8ccc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#想使用 Python 源文件，只需在另一个源文件里执行 import 语句\n",
    "import sys\n",
    " \n",
    "print('命令行参数如下:')\n",
    "for i in sys.argv:\n",
    "   print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7e4bdda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#简单介绍使用numpy库创建数组的几种方式\n",
    "#使用 arange 函数创建数值范围并返回 ndarray 对象\n",
    "x = np.arange(3)  \n",
    "print ('x=',x)\n",
    "#创建一个指定形状（shape）、数据类型（dtype）且未初始化的数组\n",
    "y = np.empty(3,dtype = int) \n",
    "print ('y=',y)\n",
    "#创建指定大小的数组，数组元素以 0 来填充\n",
    "z = np.zeros(3, dtype = int) \n",
    "print ('z=',z)\n",
    "#创建一个一维数组，数组是一个等差数列构成的\n",
    "a = np.linspace(1,13,3)\n",
    "print ('a=',a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73259f8e",
   "metadata": {},
   "source": [
    "# 二、数据处理示例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcfc3a2c",
   "metadata": {},
   "source": [
    "diabetes.csv是一个亚利桑那州的Pima Womans Hospital包含糖尿病相关测量和结果的历史记录的数据集，目的是根据某些诊断测量结果预测患者是否患有糖尿病。该数据集由8个医学预测变量和一个目标变量outcome组成。在进行机器学习的模型训练前我们需要对原始数据进行一定的处理，下列即为一个简略的数据处理示例过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85f05918",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">1)引入各种常用库</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "662450cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sb\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.model_selection import train_test_split\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "465cd6d0",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">2)使用Pandas加载数据文件并输出文件信息</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "962f7b0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用Pandas将CSV文件加载到名为pima的数据框对象中\n",
    "pima = pd.read_csv(\"diabetes.csv\")\n",
    "#输出文件信息\n",
    "print(pima.dtypes)\n",
    "#输出csv文件中每列缺失的项目数量\n",
    "print(pima.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36a23574",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">3)数据查看</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "095cde5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出前10行数据\n",
    "print(pima.head(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af0b46f2",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">4)替换缺失值为0</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88a4884e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "pima[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']].replace(np.NaN,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e6f4007",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">5)将Outcome这一列内容可视化</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "588cce42",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(5,4))\n",
    "sb.countplot(pima['Outcome'],palette='OrRd_r',saturation=0.8)\n",
    "plt.tight_layout()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6b1d6e6",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">6)将Pregnancies这列连续变量分别按不同划分方法转换为分类变量</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c1578c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将Pregnancies这一列的内容按照[0,1,5,10,250]的区间分别划分为[0,1,2,3]的label\n",
    "Pregnancies1=pd.cut(pima['Pregnancies'],[0,1,5,10,250], labels=[0,1,2,3],right=False)\n",
    "#将Pregnancies这一列的内容按照四等分划分为[0,1,2,3]的label\n",
    "Pregnancies2=pd.qcut(pima['Pregnancies'],4, labels=[0,1,2,3])\n",
    "print(Pregnancies1.head(10))\n",
    "print(Pregnancies2.head(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e12dd773",
   "metadata": {},
   "source": [
    "<span style=\"color:purple\">7)将原始数据集划分为训练集和测试集</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "959ef96d",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = pima.drop(columns='Outcome')\n",
    "y1 = pima['Outcome']\n",
    "x_train1, x_test1, y_train1, y_test1 = train_test_split(x1, y1, test_size = 0.25, random_state=0)\n",
    "#输出划分后的两个集合和原始集合的大小\n",
    "print(x1.shape, x_train1.shape, x_test1.shape, y1.shape, y_train1.shape, y_test1.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35cc0f5c",
   "metadata": {},
   "source": [
    "# 三、课堂练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da68bbb4",
   "metadata": {},
   "source": [
    "在jupyter notebook工具上使用python编写一个冒泡排序算法，要求如下：  \n",
    "1)待排序数组手动输入   \n",
    "2)默认排序方式为降序，可以按自身需求提供排序方式的选择  \n",
    "3)将所实现算法编写为一个可调用函数，然后在另一个模块调用   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0b92f50",
   "metadata": {},
   "source": [
    "# 四、课堂拓展"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec7f7b86",
   "metadata": {},
   "source": [
    "为了方便同学们课后自学，向大家介绍一些相关网站帮助同学们自行学习提升：    \n",
    "https://docs.python.org/zh-cn/3/tutorial/index.html  \n",
    "https://www.w3school.com.cn/python/index.asp  \n",
    "https://www.runoob.com/python3/  \n",
    "https://www.numpy.org.cn/  \n",
    "http://www.pypandas.cn/docs/  \n",
    "https://seaborn.apachecn.org/#/README  \n",
    "https://scikit-learn.org.cn/  \n",
    "https://jupyter-notebook.readthedocs.io/en/stable/notebook.html"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
